package com.ning.mvvmplayandroid.util

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 作者： ch
 * 时间： 2018/6/8 0008-下午 4:20
 * 描述：
 * 来源：
 */


object DateUtils {
    private val ONE_MINUTE = 60000L
    private val ONE_HOUR = 3600000L
    private val ONE_DAY = 86400000L
    private val ONE_WEEK = 604800000L

    private val ONE_SECOND_AGO = "秒前"
    private val ONE_MINUTE_AGO = "分钟前"
    private val ONE_HOUR_AGO = "小时前"
    private val ONE_DAY_AGO = "天前"
    private val ONE_MONTH_AGO = "月前"
    private val ONE_YEAR_AGO = "年前"


    fun format(date: Date): String {
        val delta = System.currentTimeMillis() - date.time
        if (delta < 1L * ONE_MINUTE) {
            val seconds = toSeconds(delta)
            return (if (seconds <= 0) 1 else seconds).toString() + ONE_SECOND_AGO
        }
        if (delta < 45L * ONE_MINUTE) {
            val minutes = toMinutes(delta)
            return (if (minutes <= 0) 1 else minutes).toString() + ONE_MINUTE_AGO
        }
        if (delta < 24L * ONE_HOUR) {
            val hours = toHours(delta)
            return (if (hours <= 0) 1 else hours).toString() + ONE_HOUR_AGO
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天"

        }
        if (delta < 30L * ONE_DAY) {
            val days = toDays(delta)
            return (if (days <= 0) 1 else days).toString() + ONE_DAY_AGO
        }
        if (delta < 12L * 4L * ONE_WEEK) {
            val months = toMonths(delta)
            return (if (months <= 0) 1 else months).toString() + ONE_MONTH_AGO
        } else {
            val years = toYears(delta)
            return (if (years <= 0) 1 else years).toString() + ONE_YEAR_AGO
        }
    }

    private fun toSeconds(date: Long): Long {
        return date / 1000L
    }

    private fun toMinutes(date: Long): Long {
        return toSeconds(date) / 60L
    }

    private fun toHours(date: Long): Long {
        return toMinutes(date) / 60L
    }

    private fun toDays(date: Long): Long {
        return toHours(date) / 24L
    }

    private fun toMonths(date: Long): Long {
        return toDays(date) / 30L
    }

    private fun toYears(date: Long): Long {
        return toMonths(date) / 365L
    }

    fun getTime(time: Long): String {
        val format = SimpleDateFormat("yyyy-MM-dd")
        return format.format(Date(time))
    }

    fun getDate(time: Long): String {
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return format.format(Date(time))
    }

    fun getHours(time: Long): String {
        val format = SimpleDateFormat("HH:mm")
        return format.format(Date(time))
    }


    fun getDate(time: String): Long {
        if (time.contains("-") && time.contains(":")) {
            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            try {
                return format.parse(time)!!.time
            } catch (e: ParseException) {
                e.printStackTrace()
                return 0
            }

        }
        return 0
    }


    fun getDatesByToday(time: String): Double {
        val today = System.currentTimeMillis()
        val dates = getDate(time)
        return (today - dates).toDouble() / 3600.0 / 24.0 / 1000.0

    }

    /*
     * 将时间转换为时间戳
     */
    @Throws(ParseException::class)
    fun dateToStamp(s: String): String {
        val res: String
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val date = simpleDateFormat.parse(s)
        val ts = date!!.time
        res = ts.toString()
        return res

    }
}
